/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package xxhci.oracle.apps.XXIERP.SalaryAdvance.webui;

import com.sun.java.util.collections.HashMap;

import java.io.Serializable;

import java.text.DecimalFormat;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageDateFieldBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageLovInputBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;

import oracle.jbo.Row;
import oracle.jbo.domain.Date;
import oracle.jbo.domain.Number;

import xxhci.oracle.apps.XXIERP.SalaryAdvance.server.EmpVORowImpl;


/**
 * Controller for ...
 */
public class xxierpsalaryadvSearchCO extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = 
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);
        System.out.println("LoginName:" + pageContext.getUserName());
        System.out.println("RESPID:" + pageContext.getResponsibilityId());
        System.out.println("RESPNAME:" + pageContext.getResponsibilityName());
        
        OAApplicationModule am = pageContext.getApplicationModule(webBean);

        Serializable[] initParams = 
        { pageContext.getUserName().toString(), Date.getCurrentDate().toString() };
        am.invokeMethod("getEmpDetails", initParams);
        OAViewObject vo = (OAViewObject)am.findViewObject("EmpVO1");
        Row row[] = vo.getAllRowsInRange();
        
        if(vo.getRowCount()!=0)
                {
        Number PersonId = (Number)row[0].getAttribute("PersonId");
        String strWhereClause ="";
        if( pageContext.getResponsibilityName().toString().equalsIgnoreCase("Employee Self-Service"))
          strWhereClause=  "(EMPLOYEE_NO= " + PersonId.toString() + ")";
          else if(pageContext.getResponsibilityName().toString().equalsIgnoreCase("Manager Self-Service"))
              strWhereClause=  "(RECOMMEND_EMPLOYEE_ID=" + 
                PersonId.toString() + 
                " AND STATUS_ID<>'SA01' ) OR (APPROVER_EMPLOYEE_ID=" + 
                PersonId.toString() + " AND STATUS_ID<>'SA01' )";
        else
            strWhereClause=  "(EMPLOYEE_NO= " + PersonId.toString() + ") OR (RECOMMEND_EMPLOYEE_ID=" + 
              PersonId.toString() + 
              " AND STATUS_ID<>'SA01' ) OR (APPROVER_EMPLOYEE_ID=" + 
              PersonId.toString() + " AND STATUS_ID<>'SA01' )";
              
        Serializable[] initParams1 = { strWhereClause };
        am.invokeMethod("getRequest", initParams1);
        OAViewObject saVO = (OAViewObject)am.findViewObject("XxierpSalaryAdvVO1");
              
        OAMessageTextInputBean LoginEmpId = 
            (OAMessageTextInputBean)webBean.findChildRecursive("LoginEmpId");
        LoginEmpId.setValue(pageContext, PersonId);
                    OAMessageDateFieldBean SalAdvReqDate = 
                        (OAMessageDateFieldBean)webBean.findChildRecursive("SalAdvReqDate");
                        SalAdvReqDate.setReadOnly(true);
                    OAMessageLovInputBean Status = 
                        (OAMessageLovInputBean)webBean.findChildRecursive("Status");
                        Status.setReadOnly(true);
                 //   OAViewObject saVO = (OAViewObject)am.findViewObject("XxierpSalaryAdvVO1");
                    Row row1;
                    double sum=0;
                    DecimalFormat df = new DecimalFormat("###.##");
                    while ((row1=saVO.next()) != null)
                   {  
                       if(row1.getAttribute("EmployeeNo").equals(PersonId) && !row1.getAttribute("StatusId").equals("SA05") ) {
                           double outStandingAmt= Float.parseFloat(row1.getAttribute("OutstandingAmt").toString());
                            sum=sum+outStandingAmt;
                       }
                   }
                    OAMessageTextInputBean OutStandingAmt = 
                        (OAMessageTextInputBean)webBean.findChildRecursive("OutStandingAmt");
                    OutStandingAmt.setValue(pageContext, df.format(sum)); 
                  
                }
                else {
                    OAException successMessage = 
                                             new OAException("No Employee Details with user "+pageContext.getUserName().toString(), 
                                                             OAException.ERROR);
                                         pageContext.putDialogMessage(successMessage);
                }
    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, 
                                   OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);

        OAApplicationModule am = pageContext.getApplicationModule(webBean);
        
        String strParamAction = pageContext.getParameter(EVENT_PARAM);
        String strParamReqNo = pageContext.getParameter("paramSalAdvReqNo");
        /**/String strParamStatusId = 
            pageContext.getParameter("paramStatusId");
        /**/String strParamStatusDesc = 
            pageContext.getParameter("paramStatusDesc");
        /**/String strParamPersonId = 
            pageContext.getParameter("paramEmployeeId");
        /**/String strParamRecommenderId = 
            pageContext.getParameter("paramRecommendEmployeeId");
        /**/String strParamApproverId = 
            pageContext.getParameter("paramApproverEmployeeId");

        OAMessageTextInputBean LoginEmpId = 
            (OAMessageTextInputBean)webBean.findChildRecursive("LoginEmpId");
        String strLoginEmpId = LoginEmpId.getValue(pageContext).toString();

        HashMap phm = new HashMap();
        phm.put("xxierpParamReqNo", strParamReqNo);
        phm.put("xxierpParamAction", strParamAction);
        /**/phm.put("xxierpParamStatusId", strParamStatusId);
        /**/phm.put("xxierpParamPersonId", strParamPersonId);
        /**/phm.put("xxierpParamLoginEmpId", strLoginEmpId);
        /**/phm.put("xxierpParamStatusDesc", strParamStatusDesc);
        /**/phm.put("xxierpParamRecommenderId", strParamRecommenderId);
        /**/phm.put("xxierpParamApproverId", strParamApproverId);
        OAMessageTextInputBean OutStandingAmt = 
            (OAMessageTextInputBean)webBean.findChildRecursive("OutStandingAmt");
         double amt= Float.parseFloat(OutStandingAmt.getValue(pageContext).toString()); 
        if((pageContext.getParameter("event").equalsIgnoreCase("CreateRequest") || 
            strParamAction.equals("CreateRequest"))&& amt!=0) {
            OAException successMessage = 
                                     new OAException("Request Cannot be Processed until Previous Salary Advance is Cleared. ", 
                                                     OAException.ERROR);
                                 pageContext.putDialogMessage(successMessage);
        }
       else if (pageContext.getParameter("event").equalsIgnoreCase("ViewRequest") || 
            strParamAction.equals("ViewRequest") || 
            pageContext.getParameter("event").equalsIgnoreCase("CreateRequest") || 
            strParamAction.equals("CreateRequest")) {    
            pageContext.setForwardURL("OA.jsp?page=/xxhci/oracle/apps/XXIERP/SalaryAdvance/webui/xxierpsalaryadvPG", 
                                      null, 
                                      OAWebBeanConstants.KEEP_MENU_CONTEXT, 
                                      null, phm, true, 
                                      OAWebBeanConstants.ADD_BREAD_CRUMB_YES, 
                                      OAWebBeanConstants.IGNORE_MESSAGES);
        }
    }
}
